import { ref } from '@vue/reactivity'

export default function(totalCount: number, text?: string){
  const defaultText = text || '发送验证码'
  const timer = ref(0)
  const countDownText = ref(defaultText)

  const run = () => {
    countDownText.value = `${timer.value}S`
    timer.value = timer.value -1
    setTimeout(() => {
      if(timer.value === 0){
        countDownText.value = defaultText
        return
      }else{
        run()
      }
    }, 1000)
  }

  const startCountDown = (callback: Function) => {
    if(countDownText.value !== defaultText){
      return
    }
    callback()
    timer.value = totalCount
    run()
  }
  return {
    countDownText,
    startCountDown
  }
}

